package iotwisp.school.system.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import iotwisp.school.system.command.BindImageToBizSCmd;
import iotwisp.school.system.command.CreateImageSCmd;
import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.util.UUIDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * ImageVO ImageSPI ImageViewSPI ImageService ImageViewService ImageController
 * ImageViewController
 *
 * @author yuxx
 */
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "image")
@SuppressWarnings("serial")
public class Image extends StringIdBaseEntity {

	/**
	 * 文件名
	 */
	@Column(name = "title", length = 128)
	private String title;

	/**
	 * 归属主体id
	 */
	@Column(name = "owner_id", length = 32)
	private String ownerId;

	/**
	 * 归属主体类型
	 */
	@Column(name = "owner_type", length = 32)
	private String ownerType;

	/**
	 * 可访问相对url
	 */
	@Column(name = "url", length = 512)
	private String url;

	/**
	 * 文件元数据
	 */
	@Column(name = "metadata_json", length = 2048)
	private String metadataJSON;

	/**
	 * 创建时间
	 */
	@Column(name = "create_date", columnDefinition = M.DATE_COLUMN)
	private Date createDate;

	/**
	 * 是否已删除
	 */
	@Type(type = "yes_no")
	@Column(name = "remove")
	private Boolean remove;

	/**
	 * 类型后缀
	 */
	@Column(name = "suffix")
	private String suffix;

	/**
	 * 图片多尺寸存储信息
	 */
	@Column(name = "spec_image_map_json", length = 512)
	private String specImageMapJSON;

	public void create(CreateImageSCmd cmd) {

		if (StringUtils.isNotBlank(cmd.getImageId())) {
			setId(cmd.getImageId());
		} else {
			setId(UUIDGenerator.getUUID());
		}

		setTitle(cmd.getTitle());
		setOwnerId(cmd.getOwnerId());
		setOwnerType(cmd.getOwnerType());
		setUrl(cmd.getUrl());
		setMetadataJSON(cmd.getMetadataJSON());
		setSuffix(cmd.getSuffix());
		setCreateDate(new Date());
		setRemove(false);
		setSpecImageMapJSON(cmd.getSpecImageMapJSON());
	}

	public void bindToBiz(BindImageToBizSCmd cmd) {
		setOwnerId(cmd.getOwnerId());
		setOwnerType(cmd.getOwnerType());
	}

}
